# -*- coding: utf-8

from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register
from django.utils.translation import ugettext as _
from EssaiAVA.ava.models import *
from django.core.mail import send_mail
from EssaiAVA.ava.outils.message import *

def set_choices_detail_dajax(invit_id2):
    invit_id = int(invit_id2)
    invit = Invitation.objects.get(id=invit_id)
    # rend les details de l'invitation correspondante formatés pour l'appel Dajax
    questions = invit.question_set.all()
    dict = {}
    i = 0
    for quest in questions:
        if quest.champ=="Type de restaurant":
            sous_li = []                        
            li = ['FF', _(u'Fast-Food')]
            sous_li.append(li)
            li = ['AS', _(u'Asiatique')]
            sous_li.append(li)
            li = ['FR', _(u'Français')]
            sous_li.append(li)
            li = ['IT', _(u'Italien')]
            sous_li.append(li)
            li = ['OR', _(u'Oriental')]
            sous_li.append(li)
            dict['GO'] = sous_li
        elif quest.champ=="Budget":
            sous_li = []            
            li = ['MC', _(u'Moins cher')]
            sous_li.append(li)
            li = ['PC', _(u'Plus cher')]
            sous_li.append(li)
            dict['BU'] = sous_li
        elif quest.champ=="Distance":
            sous_li = []
            li = ['ML', _(u'Moins loin')]
            sous_li.append(li)
            li = ['PL', _(u'Plus loin')]
            sous_li.append(li)
            dict['DI'] = sous_li
        else:
            sous_li = []
            tag_quest = 'CU' + str(i)
            j = 0
            for reponse in quest.reponse_set.all():
                tag_rep = tag_quest + 'R' + str(j)
                li = [tag_rep, reponse.champ]
                sous_li.append(li)
                j+=1
            dict[tag_quest] = sous_li
            i+=1
    return dict

def set_choices_type_dajax(invit): 
    # rend le tuple de type possible pour le sondage de l'invitation formaté pour Dajax
    questions = invit.question_set.all()
    rep = []
    i = 0
    for quest in questions:
        if quest.champ=="Type de restaurant":
            tuple = ['GO', _('Type de restaurant')]
            rep.append(tuple)
        elif quest.champ=="Budget":
            tuple = ['BU', _(u'Budget')]
            rep.append(tuple)
        elif quest.champ=="Distance":
            tuple = ['DI', _(u'Distance')]
            rep.append(tuple)
        else:
            tag = 'CU' + str(i)
            tuple = [tag, quest.champ]
            rep.append(tuple)
            i+=1
    return rep

@dajaxice_register
def updatecombo(request, option, id, invit_id):
    for o in id:
        if o.isdigit():
            num = o
            break
    dajax = Dajax()
    options = set_choices_detail_dajax(invit_id)
    out = ""
    for o in options[option]:
        out += "<option value=%s>%s</option>" % (o[0], o[1])
    ref = '#id_form-%s-detail'%(num)
    dajax.assign(ref,'innerHTML',out)
    return dajax.json() 

@dajaxice_register
def updateselected(request, selected, id, invit_id):
    for o in id:
        if o.isdigit():
            num = o
            break
    dajax = Dajax()
    invit = Invitation.objects.get(id=invit_id)
    list_type = set_choices_type_dajax(invit)
    dict = set_choices_detail_dajax(invit_id)
    selected_key=""
    selected_options= []
    for key in dict.keys():
        for list in dict[key]:
            if selected in list:
                selected_key=key
                selected_options=dict[key]
    out_type = ""
    for o in list_type:
        if o[0]==selected_key:
            out_type += "<option value=%s selected='selected'>%s</option>" % (o[0], o[1])
        else:
            out_type += "<option value=%s>%s</option>" % (o[0], o[1])
    out_detail= ""
    for o in selected_options:
        if o[0]==selected:
            out_detail += "<option value=%s selected='selected'>%s</option>" % (o[0], o[1])
        else:
            out_detail += "<option value=%s>%s</option>" % (o[0], o[1])  
    ref_type = '#id_form-%s-type'%(num)
    ref_detail = '#id_form-%s-detail'%(num)
    dajax.assign(ref_type,'innerHTML',out_type)
    dajax.assign(ref_detail,'innerHTML',out_detail)
    return dajax.json()
    
    

@dajaxice_register
def rappel_restaurant(request, email, invit_id):
    invit = Invitation.objects.get(id=invit_id)
    invite = invit.invite_set.get(email=email)
    send_mail("Rappel invitation AVA",
                            message_rappel_restaurant(invit, invite),
                            "invitation.ava@gmail.com",
                            [email],
                            fail_silently=False)      
    
@dajaxice_register
def rappel_sondage(request, email, invit_id):
    invit = Invitation.objects.get(id=invit_id)
    invite = invit.invite_set.get(email=email)
    send_mail("Rappel invitation AVA",
                            message_rappel_sondage(invit, invite),
                            "invitation.ava@gmail.com",
                            [email],
                            fail_silently=False)     

@dajaxice_register
def confirm_restau(request, email, invit_id):
    invit = Invitation.objects.get(id=invit_id)
    orga = invit.invite_set.all()[0]
    invite = invit.invite_set.get(email=email)
    invite.confirm = True
    invite.save()
    send_mail("Confirmation invitation AVA",
                    message_confirm_restau(invit, invite),
                    "invitation.ava@gmail.com",
                    [orga.email],
                    fail_silently=False)
    
    
    
    

